package com.bflow.web.controller.example;

import com.bflow.api.IBflowRunInstanceService;
import com.bflow.api.IBflowRunTaskService;
import com.bflow.api.bo.*;
import com.bflow.api.bo.ext.BflowUserBo;
import com.bflow.commons.bean.PageResult;
import com.bflow.commons.bean.TableResult;
import com.bflow.util.AssertUtil;
import com.bflow.util.JsonUtil;
import com.bflow.web.controller.BflowBaseController;
import com.bflow.web.util.RequestUtil;
import com.bflow.web.util.TestLoginUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @name：议标流程
 * @author：xuhaowen
 * @date：2019-01-05
 */
@Controller
@RequestMapping(value = "/example/neg")
public class NegController extends BflowBaseController {

    @Autowired
    private IBflowRunInstanceService<?, BflowRunInstanceBo> bflowRunInstanceService;
    @Autowired
    private IBflowRunTaskService bflowRunTaskService;

    private static final String BUSI_KEY = "neg";

    /**
     * 列表
     * @param queryParam
     * @param req
     * @param resp
     * @return
     */
    @RequestMapping(value = "list")
    @ResponseBody
    public String list(BflowRunInstanceQueryParam queryParam, HttpServletRequest req, HttpServletResponse resp) {
        try {
            AssertUtil.isNotNull(TestLoginUtil.getUser(req), "您还未登录, 请先登录");
            PageResult<BflowRunInstanceBo> pageResult = bflowRunInstanceService.findPage(queryParam, BUSI_KEY);
            TableResult result = new TableResult(pageResult.getResult(),pageResult.getTotalCount());
            return JsonUtil.toStr(result);
        } catch (Exception e) {
            return returnFail(e, "列表查询失败");
        }
    }

    /**
     * 审批列表
     * @param queryParam
     * @param req
     * @param resp
     * @return
     */
    @RequestMapping(value = "assignList")
    @ResponseBody
    public String assignList(BflowRunTaskQueryParam queryParam, HttpServletRequest req, HttpServletResponse resp) {
        try {
            BflowUserBo user = TestLoginUtil.getUser(req);
            AssertUtil.isNotNull(user, "您还未登录, 请先登录");
            String actKey = "assign";
            PageResult<BflowRunTaskBo> pageResult = bflowRunTaskService.findMyTaskPage(queryParam.getPageBean(), BUSI_KEY, actKey, user.getRoleKey());
            TableResult result = new TableResult(pageResult.getResult(),pageResult.getTotalCount());
            return JsonUtil.toStr(result);
        } catch (Exception e) {
            return returnFail(e, "分派列表查询失败");
        }
    }

    /**
     * 审批列表
     * @param queryParam
     * @param req
     * @param resp
     * @return
     */
    @RequestMapping(value = "auditList")
    @ResponseBody
    public String auditList(BflowRunTaskQueryParam queryParam, HttpServletRequest req, HttpServletResponse resp) {
        try {
            BflowUserBo user = TestLoginUtil.getUser(req);
            AssertUtil.isNotNull(user, "您还未登录, 请先登录");
            boolean isValid = RequestUtil.getBooleanParameter(req, "isValid");
            String actKey = isValid?"valid":"audit";
            PageResult<BflowRunTaskBo> pageResult = bflowRunTaskService.findMyTaskPage(queryParam.getPageBean(), BUSI_KEY, actKey, isValid ? user.getUserId() : user.getRoleKey());
            TableResult result = new TableResult(pageResult.getResult(),pageResult.getTotalCount());
            return JsonUtil.toStr(result);
        } catch (Exception e) {
            return returnFail(e, "审批列表查询失败");
        }
    }


    /**
     * 查询instance业务数据
     * @param instanceId
     * @param req
     * @param resp
     * @return
     */
    @RequestMapping(value = "detail")
    @ResponseBody
    public String detail(@RequestParam String instanceId, HttpServletRequest req, HttpServletResponse resp) {
        try {
            BflowUserBo user = TestLoginUtil.getUser(req);
            AssertUtil.isNotNull(user, "您还未登录, 请先登录");
            BflowRunInstanceBo bo = bflowRunInstanceService.get(instanceId, user.getUserId(), true);
            return returnOk(bo, "查询成功");
        } catch (Exception e) {
            return returnFail(e, "查询失败");
        }
    }

    /**
     * 查询task任务数据
     * @param taskId
     * @return
     */
    @RequestMapping(value = "getTask")
    @ResponseBody
    public String getTask(@RequestParam String taskId, HttpServletRequest req, HttpServletResponse resp) {
        try {
            BflowUserBo user = TestLoginUtil.getUser(req);
            AssertUtil.isNotNull(user, "您还未登录, 请先登录");
            BflowRunTaskBo bo = bflowRunTaskService.getTaskInfo(taskId, user.getUserId(), true, true);
            return returnOk(bo, "查询成功");
        } catch (Exception e) {
            return returnFail(e, "查询失败");
        }
    }


    /**
     * 验证
     * @param bo
     */
    @RequestMapping(value = "valid")
    @ResponseBody
    public String valid(BflowRunTaskAuditBo bo, HttpServletRequest req, HttpServletResponse resp) {
        try {
            bo.setOperInfo(TestLoginUtil.getUser(req));
            bflowRunTaskService.process(bo);
            return returnOk("验证成功");
        } catch (Exception e) {
            return returnFail(e, "验证失败");
        }
    }

    /**
     * 审批
     * @param bo
     * @return
     */
    @RequestMapping(value = "audit")
    @ResponseBody
    public String audit(BflowRunTaskAuditBo bo, HttpServletRequest req, HttpServletResponse resp) {
        try {
            bo.setOperInfo(TestLoginUtil.getUser(req));
            bflowRunTaskService.process(bo);
            return returnOk("审批成功");
        } catch (Exception e) {
            return returnFail(e, "审批失败");
        }
    }


}
